﻿using System;
using System.Collections.Generic;
using System.Linq;
using ETL.Business.Models;
using ETL.Common.Logging;
using ETL.Entity;
using ETL.DataAccess;

namespace ETL.Business.Handlers.ProjectCategoryHandler
{
    public class GetProjectCategoryToList
    {
        private readonly ProjectCategoryRepository _projectCategoryRepository;

        public GetProjectCategoryToList()
        {
            _projectCategoryRepository = new ProjectCategoryRepository();
        }
        public GetProjectCategoryToList(ProjectCategoryRepository projectCategoryRepository)
        {
            _projectCategoryRepository = projectCategoryRepository;
        }

        public virtual IEnumerable<ProjectCategoryModel> Execute(int? projectId)
        {
            IEnumerable<ProjectCategoryModel> projectCategoriesModel = null;
            
            try
            {
                // try to get from db
                IEnumerable<project_categories> projectCategoriesData = 
                    _projectCategoryRepository.GetProjectCategories(projectId);

                // parse to business model
                if(projectCategoriesData != null)
                {
                    projectCategoriesModel = from prjCat in projectCategoriesData
                                             select new ProjectCategoryModel(prjCat);
                }

                return projectCategoriesModel;
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
                return null;
            }
        }
    }
}
